package com.situ.mall.controller.front;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.entity.Cart;
import com.situ.mall.entity.Category;
import com.situ.mall.entity.Order;
import com.situ.mall.entity.OrderItem;
import com.situ.mall.entity.Product;
import com.situ.mall.entity.Result;
import com.situ.mall.entity.User;
import com.situ.mall.service.ICartService;
import com.situ.mall.service.ICategoryService;
import com.situ.mall.service.IOrderService;
import com.situ.mall.service.IProductService;

@Controller
@RequestMapping("/userOrder")
public class FrontUserOrderController {
	@Autowired
	private ICartService cartService;
	@Autowired
	private ICategoryService categoryService;
	@Autowired
	private IOrderService orderService;

	@RequestMapping("/insert.shtml")
	@ResponseBody
	public Result insert(Cart cart, HttpSession session) {
		System.out.println("FrontCartController.insert()");
		User user = (User)session.getAttribute("user");
		
		cart.setUserId(user.getId());
		cart.setChecked(1);
		cart.setQuantity(1);
		System.out.println("productid:"+cart.getProductId());
		System.out.println("Cart = " + cart);
		boolean isSuccess = cartService.insert(cart);
		if (isSuccess) {
			return Result.success("插入成功");
		} else {
			return Result.error("插入失败");
		}
	}
	
	@RequestMapping("/getUserOrderPage.shtml")
	public String getUserOrderPage(HttpSession session, Model model) {
		System.out.println("FrontUserOrderController.getUserOrderPage()");
		User user = (User)session.getAttribute("user");
		System.out.println("User = " + user);
		
		List<Order> list = orderService.selectByUserId(user.getId());
		for (Order order : list) {
			System.out.println(order);
		}
		
		System.out.println("FrontUserOrderController.asdasdasdad");
		
		for (Order order : list) {
			long orderNo = order.getOrderNo();
			System.out.println("orderNo = " + orderNo);
			List<OrderItem> orderItemList = orderService.selectOrderItemByOrderId(orderNo);
			for (OrderItem orderItem : orderItemList) {
				System.out.println(orderItem);
			}
			order.setOrderItemList(orderItemList);
			System.out.println(order);
		}	
		model.addAttribute("list", list);
		
		List<Category> topCategoryList = categoryService.selectTopCategoryList();
		List<Category> secondCategoryList = categoryService.selectSecondCategoryList();
		model.addAttribute("topCategoryList", topCategoryList);
		model.addAttribute("secondCategoryList", secondCategoryList);
		
		return "user_order";
	}
	

	
}
